package org.wmine.preprocess;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import org.wmine.core.IntegerTextField;

public class Discretization extends JFrame implements ActionListener
{
	JLabel labelSelectAtrr,labelClass;
	JButton btnOK,btnDiscretize;
	JComboBox cmbAttr;
	IntegerTextField  txtClass;
	JPanel panel;
	JScrollPane scroll=null;
	JTable table;
	DefaultTableModel d;
	Object []column={"To","From","Label"};
	Object [][]data;
	DefaultTableModel dtm;
	static int dtmrow=0;
	//Iterator<Attribute> itratr;
	public Discretization(DefaultTableModel dtm)
	{
		this.dtm=dtm;
	}
	public Discretization(String []columnNames,DefaultTableModel dtm)
	{
		//itratr=new Iterator<Attribute>();
		//itratr=itrNew;
		
		this.dtm=dtm;
		dtmrow=this.dtm.getRowCount();
		
		panel=new JPanel();
		panel.setLayout(null);
		panel.setBounds(0,0,600,500);
		this.setLayout(null);
		this.setBounds(150,120,600,500);
		this.setTitle("Discretization");
		
		labelSelectAtrr=new JLabel("Select Attribute");
		labelSelectAtrr.setBounds(75,50,100,25);
		
		cmbAttr=new JComboBox();
		for(int i=0;i<columnNames.length;i++)
			cmbAttr.addItem(columnNames[i]);
		cmbAttr.setBounds(215,50,150,25);
		
		labelClass=new JLabel("How Many Classes? ");
		labelClass.setBounds(75,90,150,25);
		
		txtClass=new IntegerTextField();
		txtClass.setBounds(215,90,150,25);
		
		btnOK=new JButton("OK");
		btnOK.setBounds(225,130,100,25);
	    btnOK.addActionListener(this);
	    
	    panel.add(labelSelectAtrr);
	    panel.add(cmbAttr);
	    panel.add(labelClass);
	    panel.add(txtClass);
	    panel.add(btnOK);
		
	    panel.repaint();
	    this.add(panel);
		
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==btnDiscretize)
		{
			System.runFinalization();
			System.out.println("action");
			Object []obj=new Object[dtmrow];
			PreprocessPanel.report.append("<br>You chose to perform Discretization<br>");
			PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
			for(int i=0;i<d.getRowCount();i++)
			{
				PreprocessPanel.report.append("<br>Range:"+d.getValueAt(i,0)+" to "+d.getValueAt(i,1)+" Discretized to: "+d.getValueAt(i,2)+"<br>");
				PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
				
			}
			
			for(int j=0;j<dtmrow;j++)
			{
				int flag=0;
				float value=java.lang.Float.parseFloat(""+getValue(j,cmbAttr.getSelectedIndex()));
				for(int i=0;i<d.getRowCount();i++)
				{
					float low=java.lang.Float.parseFloat(""+d.getValueAt(i,0));
					float high=java.lang.Float.parseFloat(""+d.getValueAt(i,1));
					Object str=d.getValueAt(i,2);
					
					if( (value>=low) && (value<high))
					{
						obj[j]=str;
						PreprocessPanel.report.append("<br>Value :"+value+" ==> "+str);
						//System.out.println("j::"+j+"  obj[j]::"+obj[j]);
						flag=1;
						break;
					}
				}
				if(flag==0)
				{
					obj[j]=d.getValueAt(d.getRowCount()-1,2);
					PreprocessPanel.report.append("<br>Value :"+value+" ==> "+d.getValueAt(d.getRowCount()-1,2));
				}
				
			}
			PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
			PreprocessPanel.summary.repaint();
			AddColumn(obj,cmbAttr.getSelectedIndex());
			System.runFinalization();
			/*
			while(itratr.hasNext())
			{
				Attribute a=(Attribute)itratr.next();
				if(a.getAttributeName().equals(cmbAttr.getSelectedItem()))
				{
					a.setAttributeName("Class");
					a.setAttributeType("Real");
					break;
				}
			}
			*/
			this.dispose();
		}
		
		if(ae.getSource()==btnOK)
		{
			final int num=Integer.parseInt(txtClass.getText().trim());
			System.gc();
			System.runFinalization();
			if(scroll!=null && btnDiscretize!=null)
			{
				panel.remove(scroll);
				panel.remove(btnDiscretize);
				panel.repaint();
			}
		
			data=new Object[num][3];
			int index=cmbAttr.getSelectedIndex();
			final float min=Minimum(index);
			final float max=Maximum(index);
			System.out.println("min:"+min+"    max:"+max);
			data[0][0]=min;
			data[num-1][1]=max;
			d=new DefaultTableModel(data,column);
	 		table = new JTable(d);
	 		d.addTableModelListener(new TableModelListener()
	 		{
	 			public void tableChanged(TableModelEvent arg0) 
				{
					int col=arg0.getColumn();
					int row=arg0.getFirstRow();
					String str=""+d.getValueAt(row,col);
					
					
					if(col==0 || col==1)
					{
						if(!str.equals(null)|| !(str.equals("")))
						{
							
							for(int ii=0;ii<str.length();ii++)
							{
								if((!(str.charAt(ii)>='0' &&  str.charAt(ii)<='9')) && (!(str.charAt(ii)=='.')) )
								{	
									d.setValueAt("",row,col);
									d.setValueAt(min,0,0);
									d.setValueAt(max,num-1,1);
									
								}
							}	
						}
						
					}
					if(col==1)
					{
						System.gc();
						String s=""+d.getValueAt(row,1);
						System.out.println("value::"+d.getValueAt(row,1)+"row:"+row);
						if(row==num-1)
						{
							// handle if max value is changed
							//if(row!=0 && d.getValueAt(row-1,1)!=d.getValueAt(row,0))
								//d.setValueAt(max,num-1,1);
						}
						else
						{
						try
						{
							if(!(s.equals(null)) || !(s.equals("")))
							{
								float value=java.lang.Float.parseFloat(""+d.getValueAt(row,1));
								float to=java.lang.Float.parseFloat(""+d.getValueAt(row,0));
								if(value<min ||value>max || value<to)
								{
									JOptionPane.showMessageDialog(null,"Wrong value Entered!!");
									d.setValueAt("",row,1);
								}	
								else
									d.setValueAt(value,row+1,0);
							}
						}
						catch(NumberFormatException nfe)
						{
							//System.out.println("exception in discre:"+ nfe);
						}
						}
					}
					if(col==0)
					{
						if(row!=0 && d.getValueAt(row-1,1)!=d.getValueAt(row,0))
							d.setValueAt(d.getValueAt(row-1,1),row,0);
					}
					
				}
	 		});
			
			
			
			scroll=new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			scroll.setBounds(10,160,550,200);
			
			btnDiscretize=new JButton("Discretize");
			btnDiscretize.setBounds(225,370,100,25);
			btnDiscretize.addActionListener(this);
			
			panel.add(scroll);
			panel.add(btnDiscretize);
			panel.repaint();
		}
		
	}
	
	Object getValue(int i,int j)
	{
		return this.dtm.getValueAt(i,j);
	}
	
	void AddColumn(Object [] obj,int i)
	{
		System.gc();
		System.runFinalization();
		this.dtm.addColumn("Class",obj);
		Vector v=new Vector();
		v=this.dtm.getDataVector();
		int cnt=this.dtm.getColumnCount();
		//System.out.println("v:"+v+"  :: cnt:: "+cnt);
		Vector v1=new Vector(cnt);
		for(int ii=0;ii<cnt;ii++)
		v1.add(this.dtm.getColumnName(ii));
		System.out.println("v1::"+v1);
		v1.removeElementAt(i);
		System.out.println("v1::"+v1);
		
		Iterator itr=v.iterator();
		while(itr.hasNext())
		{
			Vector o=(Vector)itr.next();
			o.removeElementAt(i);
			System.out.println("o::"+o);
		}
		this.dtm.setDataVector(v,v1);
		//System.out.println("after chang v::"+v);
	}
	
	public float Minimum(int col)
	{
		int rows=this.dtm.getRowCount();
		float min=0;
		min=java.lang.Float.parseFloat(""+this.dtm.getValueAt(0,col));
		for(int i=1;i<rows;i++)
		{
			float value=java.lang.Float.parseFloat(""+this.dtm.getValueAt(i,col));
			if(min > value)
				min=value;
		}
		return min;
	}
	public float Maximum(int col)
	{
		int rows=this.dtm.getRowCount();
		float max=0;
		max=java.lang.Float.parseFloat(""+this.dtm.getValueAt(0,col));
		for(int i=1;i<rows;i++)
		{
			float value=java.lang.Float.parseFloat(""+this.dtm.getValueAt(i,col));
			if(max < value)
				max=value;
		}
		return max;
	}
	
	public String getSelectedColumn()
	{
		return cmbAttr.getSelectedItem().toString();
	}
}

